package response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *   重定向
 *  访问MyResponse1会自动跳转到MyResponse2中
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-03-22-15:06 15:06
 */
@WebServlet("/myResponse1")
public class MyResponse1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*//1.设置状态码,重定向的状态码为302
        response.setStatus(302);
        //设置响应头：setHeader(String name,String value),
        //虚拟路径(使用request.getContextPath()进行动态获取)+资源路径
        response.setHeader("location","/day09_Servlet_war_exploded/myResponse2");*/

        //简单设置重定向的方法
        //重定向到myResponse2
        response.sendRedirect(request.getContextPath()+"/myResponse2");
        //重定向到百度
        //response.sendRedirect("https://www.baidu.com");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
